﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GFrame.Data;
using GFrame.Shadow;
using GFrame.Shadow.Search;
using EZCRS.HCMS.Entity;

namespace EZCRS.HCMS.Bll.baseinfo
{
    public class HCRS_TA_BLL
    {
        private ShadowSearch<HCRS_TA> _search;

        private ShadowSearch<HCRS_TA_Channel> _searchChannel;

        private Shadow<HCRS_TA> _shadow;
        private Shadow<HCRS_TA_Channel> _shadowChannel;

        public HCRS_TA_BLL()
        {
            _search = new ShadowSearch<HCRS_TA>();
            _search.ConnectionName = MainBase.CONNECTION_STRING;

            _searchChannel = new ShadowSearch<HCRS_TA_Channel>();
            _searchChannel.ConnectionName = MainBase.CONNECTION_STRING;

        }

        public HCRS_TA GetItem(string IATA)
        {
            
            ParamList pl = new ParamList();

            pl.Add("@iata", System.Data.SqlDbType.VarChar, IATA);

            List<HCRS_TA> lst = _search.Search("IATA = @iata", pl);

            if (lst.Count > 0) return lst[0];

            return null;
        }

        public List<HCRS_TA> GetAll()
        {
            return _search.Search(string.Empty);
        }

        public List<HCRS_TA> GetItemsByChannel(int ChannelID)
        {
            return _search.Search("IATA IN(SELECT IATA FROM HCRS_TA_CHANNEL WHERE Channel = " + ChannelID .ToString() + ")");
        }

        /// <summary>
        /// 保存对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Add(HCRS_TA obj)
        {
            if (string.IsNullOrEmpty(obj.IATA))
                obj.IATA = CreateNewIATA(obj);

            _shadow = new Shadow<HCRS_TA>(obj);
            _shadow.ConnectionName = MainBase.CONNECTION_STRING;

            return _shadow.Save();
            
        }

        /// <summary>
        /// 根据ID更新
        /// </summary>
        /// <param name="IATA">IATA</param>
        /// <param name="newobj">参数</param>
        /// <returns></returns>
        public bool Update(string IATA, HCRS_TA newobj)
        {
            ParamList pl = new ParamList();

            pl.Add("@iata", System.Data.SqlDbType.VarChar, IATA);

            List<HCRS_TA> lst = _search.Search("IATA = @iata", pl);

            if (lst.Count > 0)
            {
                HCRS_TA obj = lst[0];

                _shadow = new Shadow<HCRS_TA>(obj);

                obj.Fax = newobj.Fax;
                obj.CrateDate = newobj.CrateDate;
                obj.Email = newobj.Email;
                obj.IATA = newobj.IATA;
                obj.Linkman = newobj.Linkman;
                obj.Name = newobj.Name;
                obj.Remark = newobj.Remark;
                obj.Tel = newobj.Tel;

                _shadow.ConnectionName = MainBase.CONNECTION_STRING;

                return _shadow.Save();

            }

            return false;
        }

        /// <summary>
        /// 建立一个新的IATA号
        /// </summary>
        /// <returns></returns>
        private string CreateNewIATA(HCRS_TA obj)
        {
            

            List<HCRS_TA> lst = _search.Search(" 1=1 ORDER BY ID DESC");

            int iLastID = 0;

            if (lst.Count > 0)
            {
                HCRS_TA taobj = lst[0];
                iLastID = taobj.ID + 1;
            }
            else
            {
                iLastID++;
            }

            if (!string.IsNullOrEmpty(obj.Name))
            {

                string py = CHS2PinYin.Convert(obj.Name, ",", false);
                
                string strPY = null;

                string [] sp = py.Split(',');

                if (sp.Length >= 4)
                {
                    foreach (string s in sp)
                        strPY += s.Substring(0, 1);

                    return strPY.Substring(0, 4).ToUpper() + iLastID.ToString().PadLeft(6, '0');
                }
                else
                {

                    for (int i = 0; i < py.Length; i++)
                    {
                        string sub = py.Substring(i, 1);

                        if(sub != " " && sub != ",")
                            strPY += sub;

                        if (strPY.Length >= 4) break;
                    }

                    return strPY.Substring(0, 4).ToUpper() + iLastID.ToString().PadLeft(6, '0');
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// 检查IATA号是否存在
        /// </summary>
        /// <param name="IATA"></param>
        /// <returns></returns>
        public bool IsExistsIATA(string IATA)
        {
            ParamList pl = new ParamList();

            pl.Add("@iata", System.Data.SqlDbType.VarChar, IATA);

            List<HCRS_TA> lst = _search.Search("IATA = @iata", pl);

            if (lst.Count > 0)
                return true;

            return false;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="RowCount"></param>
        /// <returns></returns>
        public List<HCRS_TA> Search(int ChannelID, string Name, int PageIndex, int PageSize, out int RowCount)
        {

            ParamList pl = new ParamList();

            ShadowRule sr = new ShadowRule();
            sr.PageIndex = PageIndex;
            sr.PageSize = PageSize;

            string exp = "IATA IN(SELECT IATA FROM HCRS_TA_CHANNEL WHERE Channel = @channel)";

            pl.Add("@channel", System.Data.SqlDbType.Int, ChannelID);

            if (!string.IsNullOrEmpty(Name))
            {
                exp += " AND TA_Name Like @name ";

                pl.Add("@name", System.Data.SqlDbType.VarChar, "%" + Name + "%");
                
            }

            List<HCRS_TA> lst =
             _search.Search(exp, sr, pl);

            RowCount = sr.RecordCount;

            return lst;

        }

        /// <summary>
        /// 删除IATA
        /// </summary>
        /// <param name="IATA">IATA列表,1,2,3,4</param>
        public void DeleteTA(string IATA)
        {
            IATA = IATA.TrimEnd(',');

            foreach (string iata in IATA.Split(','))
            {

                ParamList pl = new ParamList();

                pl.Add("@iata", System.Data.SqlDbType.VarChar, iata);

                List<HCRS_TA> lst = _search.Search("IATA = @iata", pl);

                foreach (HCRS_TA ta in lst)
                {
                    _shadow = new Shadow<HCRS_TA>(ta);
                    _shadow.ConnectionName = MainBase.CONNECTION_STRING;

                    _shadow.Delete();
                }

            }

        }

        /// <summary>
        /// 删除TA渠道表记录
        /// </summary>
        /// <param name="IATA"></param>
        /// <returns></returns>
        public bool DeleteTAChannel(string IATA)
        {
            List<HCRS_TA_Channel> lstObj = GetChannelByIATA(IATA);

            foreach (HCRS_TA_Channel cl in lstObj)
            {
                _shadowChannel = new Shadow<HCRS_TA_Channel>(cl);

                _shadowChannel.ConnectionName = MainBase.CONNECTION_STRING;

                _shadowChannel.Delete();
            }

            return true;
        }

        /// <summary>
        /// 获取TA的CHANNEL信息
        /// </summary>
        /// <param name="IATA"></param>
        /// <returns></returns>
        public List<HCRS_TA_Channel> GetChannelByIATA(string IATA)
        {
            ParamList pl = new ParamList();

            pl.Add("@iata", System.Data.SqlDbType.VarChar, IATA);

            return _searchChannel.Search("IATA = @iata", pl);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lst"></param>
        /// <returns></returns>
        public bool Add_TA_Channel(List<HCRS_TA_Channel> lst, HCRS_TA ta)
        {
            foreach (HCRS_TA_Channel tac in lst)
            {
                if (string.IsNullOrEmpty(tac.IATA)) tac.IATA = ta.IATA;

                _shadowChannel = new Shadow<HCRS_TA_Channel>(tac);

                _shadowChannel.ConnectionName = MainBase.CONNECTION_STRING;

                _shadowChannel.Save();
            }


            return true;
        }
    }
}
